*******************************************************************************
* Loop over data sets
*******************************************************************************

local j = 1
foreach data in gallup anes gss {

*******************************************************************************
* Load data
*******************************************************************************

use dataset_`data', clear

*******************************************************************************
* Re-format for export
*******************************************************************************

if "`data'" == "gallup" local y confidence
if "`data'" == "anes" local y ft
if "`data'" == "gss" local y spend_lawenf

tostring N*, replace format(%20.0fc) force
tostring `y'*, replace format(%20.02fc) force
local groups All White Black Republican Democrat White_Republican ///
	White_Democrat Black_Democrat
foreach g in `groups' {
	replace `y'`g' = `y'`g' + " (" + N`g' + ")" if `y'`g' != "."
	replace `y'`g' = "" if `y'`g' == "."
}

local myvars year `y'All `y'White `y'Black `y'Republican ///
	`y'Democrat `y'White_Republican ///
	`y'White_Democrat `y'Black_Democrat
keep `myvars'
order `myvars'

*******************************************************************************
* Export table
*******************************************************************************

#delimit;

listtex
	`myvars' 
		using "_output/table`j'.tex"
		,
			replace
			type 
			rstyle(tabular) 	
			head(
			\begin{tabular}{lllllllll}
			\toprule 
			Year & 
			All & 
			White & 
			Black & 
			Rep & 
			Dem & 
			White Rep &
			White Dem &
			Black Dem \\
			\midrule
			)
		foot("\bottomrule\end{tabular}")
		;

#delimit cr

local j = `j' + 1

*******************************************************************************
* Close loop
*******************************************************************************

}

*******************************************************************************
* End
*******************************************************************************
